package com.irdstudio.efp.esb.api.rest.psd.apply;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.exception.BizException;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.api.bean.psd.apply.req.PsdApplyBackReqBean;
import com.irdstudio.efp.esb.api.common.EsbRespServiceHandler;
import com.irdstudio.efp.esb.api.common.GroupeInterface;
import com.irdstudio.efp.esb.api.rest.EsbController;
import com.irdstudio.efp.esb.api.service.psd.apply.PsdApplyBackService;
import com.irdstudio.efp.esb.common.server.req.EsbReqServiceBean;
import com.irdstudio.efp.esb.common.server.resp.EsbRespServiceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/esb")
public class PsdApplyBackController implements EsbController {
    /**
     * 日志信息
     */
    private final Logger logger = LoggerFactory.getLogger(PsdApplyBackController.class);

    @PostMapping("/SERVICE_30220007_06")
    public EsbRespServiceBean doRecv(@Validated @ModelAttribute("esbReqServiceBean") EsbReqServiceBean esbReqServiceBean, BindingResult bindingResult1,
                                     @Validated({GroupeInterface.psd.class}) @ModelAttribute("psdApplyBackReqBean") PsdApplyBackReqBean psdApplyBackReqBean, BindingResult bindingResult2, Exception exception) {
        // 全局关联流水号
        String glblSrlNo = esbReqServiceBean.getSysHead().getGlblSrlNo();
        try {
            // 参数校验
            validateBean(esbReqServiceBean, bindingResult1, psdApplyBackReqBean, bindingResult2, exception, logger);
            // 授信申请流水号
            String crdtAppFlowNo = psdApplyBackReqBean.getUseCrdtAppSrlNo();
            logger.info("======>调用接口【" + glblSrlNo + "30220007_06】开始<======");

            // 设置系统头对象
            setSysHeadFieldThreadLocal(esbReqServiceBean);

            psdApplyBackService.processApply(psdApplyBackReqBean);

            // 执行授信请求流程并返回
            return EsbRespServiceHandler.esbSuccessHandler(esbReqServiceBean);

        } catch (BizException e) {
            e.printStackTrace();
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        } catch (ESBException e) {
            e.printStackTrace();
            return EsbRespServiceHandler.esbFailureHandler(esbReqServiceBean, e.getErrorCode(), e.getMessage());
        } finally {
            logger.info("======>调用接口【" + glblSrlNo + "_30220007_06】结束<======");
        }
    }

    @ModelAttribute("esbReqServiceBean")
    public EsbReqServiceBean getReqEsbHeadBean(HttpServletRequest request) {
        logger.info("获取 ESB请求系统头和请求应用头对象!");
        EsbReqServiceBean esbReqServiceBean = (EsbReqServiceBean) request.getAttribute("esbReqServiceBean");
        return esbReqServiceBean;
    }

    @ModelAttribute("psdApplyBackReqBean")
    public PsdApplyBackReqBean getReqEsbBodyBean(HttpServletRequest request) {
        logger.info("获取 ESB请求体！");
        String esbReqBody = (String) request.getAttribute("esbReqBody");
        return JSONObject.parseObject(esbReqBody, PsdApplyBackReqBean.class);
    }

    @Autowired
    private PsdApplyBackService psdApplyBackService;
}